/**
 * test için
 * @ignore
 */
createInfoStore = function(n){
    var data = [];
    for (var i = 1; i<=n; i++)
        data.push({
        	id: i,
        	featureMember: 'Feature' + i,
        	rowsCount: i*2
        });

    return new Ext.data.Store({
        model: 'NG.models.Info',
        //sorters: 'featureMember',
        data: data
    });
};

/**
 * test için
 * @ignore
 */
createInfoDetailStore = function(n){
    var data = [];
    for (var i = 1; i<=n; i++)
        data.push({
        	id: i,
        	//PENDIK_UTM için
        	//valueList: i == 1 ? '444706.42480543 4539542.9139256' : '455402.91707432 4535509.7757615',
        	
        	//BAKAN projesi için
        	valueList: i == 1 ? '32.48177 37.87759' : '35.48177 38.87759',
        	objectID: "id" + i*2
        });

    return new Ext.data.Store({
        model: 'NG.models.InfoDetail',
        //sorters: 'featureMember',
        data: data
    });
};


/**
 * @class NG.views.SearchView 
 * @extends {Ext.Panel}
 * This panel used for displaying search results of map queries.
 */
NG.views.SearchView = Ext.extend(Ext.Carousel, {

	indicator: false,
	/**
	 * 
	 * @return {NG.util.List}
	 */
	createInfoList: function () {
		/*
		var store = createInfoStore(10);
		*/
		var store = new Ext.data.Store({
	        model: 'NG.models.Info',
	        //sorters: 'featureMember',
	        data: []
	    });
		/*
		store.on('load', function (store, records, successful){
    		alert('loaded');
    	    if (app.getViewport())
    	    	app.getViewport().getSearchToolbarButton().setBadge(records.length);
		});
		*/
		var tpl = new Ext.XTemplate(
			'{featureMember}  ',
	        '<tpl if="rowsCount &gt; 0">',
	        	'( <font style="color:#37D12C; font-weight:bold;">{rowsCount}</font> )',
	        '</tpl>',
		    '<tpl if="rowsCount == 0">',
	        	'( <font style="color:red;">{rowsCount}</font> )',
	        '</tpl>'
		);
		
        var list = new NG.util.List({
            id: 'countList',
        	scroll: 'vertical',
        	singleSelect: true,
        	minHeight: 300,
        	height: 300,
            //fullscreen: true,
            itemTpl: tpl, 
            store: store,
            listeners: {
                itemtap: function(sender, index, item, e) {
                	
                }
            },
            onItemDisclosure: function (record, btn, index) {
            	if (record.getRowsCount() == 0) //If there is no result
            		return;
                Ext.dispatch({
                    controller: app.controllers.searchView
                    ,action: 'listInfoDetail'
                    ,fad: record.getFeatureName()
                    ,info: record
                });
            	
            },            
            disclosure: function(record, btn, index) {},
            listeners: {afterrender: function (comp) {}}
        });
        //list.enableBubble('change');
        return list;
	},
	
	/**
	 * 
	 * @return {NG.util.List}
	 */
	createInfoDetailList: function (){

		var store = new Ext.data.Store({
	        model: 'NG.models.InfoDetail',
	        //sorters: 'featureMember',
	        data: [],
	        listeners: {
	        	load: function (store, records, successful){
	        		//TODO load olunca ne yapılacağını yaz
	    		} 
	        }
	    });
		
		var tpl = new Ext.XTemplate(
			'{htmlContent}'
		);		
		
        var list = new NG.util.List({
            fullscreen: true,
            cls: 'demo-list',
            itemId: 'resultList',
            ui: 'dark',
        	scroll: 'vertical',
            singleSelect: true,
            itemTpl: tpl,
            store: store,
            onItemDisclosure: function (record, btn, index) {
        		Ext.dispatch({
            		controller: app.controllers.searchView,
            		action: 'gotoMap',
            		infoDetail: record
            	});
        		app.views.mapView.clearQueryMode();
            },
            listeners: {
                //disclosure: function(record, btn, index) {},
            	itemdoubletap: function( sender, index, item, e ) {
            		sender.fireEvent('itemswipe', sender, index, item, e ); 
            		sender.deselectAll();
            	},
                //itemtap: function(sender, index, item, e) {},
            	itemswipe: function(dataView, index, item, e ) {
            		e.stopPropagation();
                    var infoDetail = dataView.getStore().getAt(index);
                    app.fadOpePopup = new Ext.Panel({
                        floating: true,
                        modal: true,
                        centered: true,
                        hideOnMaskTap: true,
                        width: 240,
                        items: [{
                            xtype: 'netgis-FadOperationList',
                            listeners: {
                            	itemtap: function(dataView, index, item, e){
                                    var record = dataView.getStore().getAt(index);
                                    Ext.dispatch({
                                		controller: app.controllers.searchView,
                                		action: 'itemAction',
                                		operation: record,
                                		infoDetail: infoDetail
                                	});
                                    app.fadOpePopup.hide();
                                }
                            }
                        }],
                        scroll: 'vertical',
                        listeners: {
                        	show: function (comp) {
                        		var list = comp.items.items[0];
                        		list.deselectAll();
                        	} 
                        }
                    });
                    app.fadOpePopup.show('pop');             		
            	} 
            }
        });
        //list.enableBubble('all');
        return list;		
	},
	
	/**
	 * @type NG.util.List
	 */
	infoList: null,
	
	/**
	 * @type NG.util.List
	 */
	infoDetailList: null,
	
	/**
	 * This is command by which search is made 
	 * @type NG.command.GetInfoEx
	 */
	getInfoExCommand:null,
	
	/**
	 * 
	 * @param {NG.command.GetInfoEx} command
	 */
	setGetInfoExCommand: function (command) {
		this.getInfoExCommand = command;
	},
	
	/**
	 * 
	 * @return NG.command.GetInfoEx
	 */
	getGetInfoExCommand: function () {
		return this.getInfoExCommand;
	},
	
	listeners: {
    	afterrender : function (comp) {
    		comp.addLists();
    	}
	},
	fullscreen: true,
    layout: 'fit',
    iconCls: 'search',
    title: L_SEARCH_PANEL_TITLE,
    dockedItems: [new Ext.Toolbar({
    				ui: 'dark',
					dock: 'top',
					layout: 'hbox',
					items: [{
						ui: 'back',
						text: L_BACK,
						dock: 'left',
						itemId: 'bkButton',
						handler: function() {
							Ext.dispatch({
								controller: app.controllers.searchView,									
								action: 'back'
							});
						}
					},
					new Ext.form.Search({
						id: 'netgis-txtSearch',
						layout: {
							layout: 'fit',
							align: 'stretch'
						},
						flex: 1,
						placeHolder: L_ENTER_TEXT,
						listeners: {
					        keyup: function(o, e) {
					            //NetGisData.carousel.setCard(0, 'slide');
					            //NetGisData.SearchCount(o, e);
					            
					        }
						}
					}),
					{
						ui: 'confirm',
						text: L_SEARCH,
						handler: function() {
							var text = Ext.getCmp('netgis-txtSearch').getValue();
							if (!Ext.isEmpty(text)) {
								Ext.dispatch({
									controller: app.controllers.searchView,									
									action: 'searchWithText',
									text: text,
									workspace: app.views.mapView.workspace
								});								
							}
						}
					}]
		})
	],
	
	/**
	 * It is used inner.
	 * @ignore
	 */
	listsAreAdded: false,
	
	/**
	 * It is used inner.
	 * @function addLists
	 * @ignore
	 */
	addLists: function (){
		if (!this.listsAreAdded) {
			this.infoList = this.createInfoList();
			this.infoDetailList = this.createInfoDetailList();
			this.getCarousel().add(this.infoList);
			this.getCarousel().add(this.infoDetailList);		
			this.listsAreAdded = true;
		}
	},
	
	/**
	 * 
	 * @param {Array} list Elements' class is {@link NG.models.Info}
	 */
	renderInfoList: function (list){
		if (!this.listsAreAdded) {
			this.addLists();
		}
		this.infoList.getStore().removeAll();
		this.infoList.getStore().loadData(list, false);
		app.getViewport().setSearchButtonBadgeText({
			infoListSize: this.infoList.getStore().getCount() + ""
		});
	},
	
	/**
	 * 
	 * @param {Array} list Elements' class is {@link NG.models.InfoDetail}
	 */
	renderInfoDetailList: function (list){
		this.infoDetailList.getStore().removeAll();
		this.infoDetailList.getStore().loadData(list, false);
		app.getViewport().setSearchButtonBadgeText({
			infoListSize: this.infoList.getStore().getCount() + "",
			infoDetailListSize: this.infoDetailList.getStore().getCount() + ""
		});
	},
	
	/**
	 * Returns panel contained by this view named as 'carousel'
	 * 
	 * @return {Ext.Panel}
	 */
	getCarousel: function (){
		return Ext.getCmp('carousel');
	},

	/**
	 * 
	 * @return {NG.util.List}
	 */
	getInfoList: function (){
		return this.infoList;
	},
	
	
	/**
	 * 
	 * @return {NG.util.List}
	 */
	getInfoDetailList: function (){
		return this.infoDetailList;
	},
	
	/**
	 * Clears all search results.
	 * @function clear
	 */
	clear: function (){
		if (this.infoList) {
			this.infoList.clear();
		}
		if (this.infoDetailList) {
			this.infoDetailList.clear();
		}
	},
	
	/**
	 * Clears all list and activate info list.
	 * @function prepareForNewSearch
	 */
	prepareForNewSearch: function (){
		this.clear();
    	this.activate(0); //activate result list (info list)
    	var tempArray = [new NG.models.Info({featureMember: this.loadingStr, rowsCount: '...'})];
    	this.infoList.getStore().loadData(tempArray, false);
	},
	
	/**
	 * @ignore
	 */
	loadingStr: '&nbsp;&nbsp;&nbsp;' + L_LOADING,
	
	/**
	 * Clears info detail list and prepares for new detail search.
	 * @function prepareForNewDetailSearch
	 */
	prepareForNewDetailSearch: function (){
		this.infoDetailList.clear();
    	this.activate(1); //activate detail list 
    	var tempArray = [];
    	var tempDetail = new NG.models.InfoDetail({});
    	tempDetail.setHTMLContent(this.loadingStr);
    	tempArray.push(tempDetail);
    	this.infoDetailList.getStore().loadData(tempArray, false);
	},

	
	/**
	 * Activates panel indexed with @param index. Index must be a positive integer.
	 * @param {Number} index
	 */
	activate: function (index) {
		this.getCarousel().setActiveItem(index, 'slide');
	},
	
	/**
	 * @ignore
	 */
	items: [
        new Ext.Panel({
	        id: 'carousel',
	        layout: 'card',
	        fullscreen: true,
	        items: []
        })
	]
});
